package com.link.base.base.common.service;

import com.link.base.base.common.dao.mybatis.mapper.AnnounceMapper;
import com.link.base.base.common.model.Announce;
import com.link.base.base.message.push.service.PushService;
import com.link.base.base.user.model.User;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import com.link.core.cllog.LCLogger;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.rmi.ServerException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  unknwon
 * @date:   2018/11/8 19:29
 * @version 1.0
 */

@Service
public class AnnounceServiceImpl extends BasicServiceImpl<Announce> implements AnnounceService {

    @Resource
    private AnnounceMapper announceMapper;
    @Resource
    private PushService pushService;

    @Override
    public BasicMapper<Announce> getBasicMapper() {
        return announceMapper;
    }

    @Override
    public List<Announce> queryPublicPage(Announce entity, Long userId) throws Exception {
        if (null == userId) {
            throw new ServerException("PUBLIC-016");
        }
        //entity.setAttr1(userId);   xml里面没找到19.04.25  libo注释
        return announceMapper.queryPublicPage(entity);
    }

    /**
     * 查看用户未阅读公告列表
     *
     * @param entity
     * @param userId 用户ID
     * @return
     * @throws Exception
     */
    @Override
    public List<Announce> queryMyAnnouncePage(Announce entity, Long userId) throws Exception {
        if (null == userId) {
            throw new ServiceException("PUBLIC-016");
        }
//        entity.setAttr1(userId);  xml里面没找到19.04.25  libo注释
        return announceMapper.queryMyAnnouncePage(entity);
    }


    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public void publish(Announce record) throws Exception {
        if (null == record.getId()) {
            throw new ServiceException("PUBLIC-007");
        }
        Announce announce = queryById(record);
        if (announce == null) {
            throw new ServiceException("COMMON-002");
        }
        //【已发布】值列表独立源代码
        String publishVal = "PUBLISHED";
        if (StringUtils.equals(announce.getAnneStatus(), publishVal)) {
            throw new ServiceException("COMMON-003");
        }
        announce.setAnneStatus(publishVal);
        //设置日期格式 'yyyy-MM-dd HH:mm:ss'
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //将当前系统日期时间转换为字符串
        String publishDate = df.format(new Date());
        announce.setPublishDate(publishDate);

        update(announce);
        pushToUser(announce);
    }

    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public void revoke(Announce record) throws Exception {
        if (null == record.getId()) {
            throw new ServiceException("PUBLIC-007");
        }
        Announce announce = queryById(record);
        if (announce == null) {
            throw new ServiceException("COMMON-002");
        }
        //【撤回】值列表独立源代码
        String revokeVal = "REVOKED";
        announce.setAnneStatus(revokeVal);
        announce.setPublishDate(null);
        update(announce);
    }

    /**
     * 推送消息
     */
    private void pushToUser(Announce record) {
        try {
            Long anneId = record.getId();
            //发送通知
            if (null == anneId) {
                return;
            }
            Announce anneExample = new Announce();
            anneExample.setPageFlag(false);
            anneExample.setId(anneId);
            List<User> userList = announceMapper.queryUserScopePage(anneExample);
            for (int i = 0; userList != null && i < userList.size(); i++) {
                User user = userList.get(i);
                Long userId = user.getId();
                //推送消息
                pushService.sampleMessageOnlyUser("新的公告：" + record.getCaptions() + "-" + record.getStartDate(), userId);
            }
        } catch (Exception e) {
            LCLogger.withException(e);
        }
    }
}
